//
//  WDGroMoreSplashManager.m
//  WDGroMore
//
//  Created by YeJary on 2025/5/19.
//

//
//  WDGroMoreSplashManager.m
//  WDGroMore
//

//
//  WDGroMoreSplashManager.m
//  WDGroMore
//

#import "WDGroMoreSplashManager.h"
#import "WDGroMoreDelegateManager.h"
#import "WDGroMoreJosnManager.h"
#import "WDGroMoreTrackManager.h"
#import <BUAdSDK/BUSplashAd.h>
#import "WDGroMoreAdHelper.h"
#import "WDBMS.h"
#import "BMSInitializer.h"

@interface WDGroMoreSplashManager () <BUSplashAdDelegate, BUSplashCardDelegate>
@property (nonatomic, strong) BUSplashAd *splashAd;
@property (nonatomic, copy) NSString *placementId;
@property (nonatomic, copy) NSString *userId;
@end

@implementation WDGroMoreSplashManager

+ (instancetype)manager {
    static WDGroMoreSplashManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[WDGroMoreSplashManager alloc] init];
    });
    return manager;
}

- (void)tryShowSplashAdWithLaunch{
    BOOL enableCold = [BMSInitializer shared].launchModel.content.firstObject.sdk_ad_splash_cool;
    BOOL enableHot  = [BMSInitializer shared].launchModel.content.firstObject.sdk_ad_splash_hot;

    NSLog(@"[Splash] Try show,enableCold=%d, enableHot=%d",enableCold, enableHot);

    if (enableCold || enableHot) {
        [self reloadSplashAd];
    } else {
        NSLog(@"❎ [Splash] Not shown due to config");
    }
}

- (void)reloadSplashAd {
    NSLog(@"[Splash] Reloading splash ad");
    self.splashAd = nil;
    self.placementId = [WDGroMoreJosnManager GroMoreSplashIdForSDKConfigFile];
    self.userId = @"";

    BUAdSlot *slot = [[BUAdSlot alloc] init];
    slot.ID = self.placementId;

    BUSplashAd *ad = [[BUSplashAd alloc] initWithSlot:slot adSize:UIScreen.mainScreen.bounds.size];
    ad.delegate = self;
    ad.cardDelegate = self;
    ad.tolerateTimeout = 3;
    ad.hideSkipButton = NO;

    self.splashAd = ad;
    [ad loadAdData];

    NSLog(@"[Splash] loadAdData called for placementId: %@", self.placementId);

    [WDGroMoreTrackManager track:BMS_EVENT_AD_REQUEST
                          params:@{BMS_PARAM_WDS:BMS_WDS_AD_REQUEST,
                                   BMS_PARAM_TAB1:@"",
                                   BMS_PARAM_TAB2:self.placementId ?: @""}
                           dType:BMS_DTYPE_AD_ACTION
                            desc:BMS_WDS_AD_REQUEST
                         options:[WDGroMoreTrackManager trackOptionsOnlySendBMS]];
    
    [WDGroMoreTrackManager track:BMS_EVENT_AD_FILL
                          params:@{BMS_PARAM_WDS:BMS_WDS_AD_FILL,
                                   BMS_PARAM_TAB1:@"",
                                   BMS_PARAM_TAB2:self.placementId ?: @""}
                           dType:BMS_DTYPE_AD_ACTION
                            desc:BMS_WDS_AD_FILL
                         options:[WDGroMoreTrackManager trackOptionsOnlySendBMS]];
}

#pragma mark - BUSplashAdDelegate

- (void)splashAdLoadSuccess:(BUSplashAd *)splashAd {
    NSLog(@"✅ [Splash] Load success");
    UIWindow *keyWindow = UIApplication.sharedApplication.keyWindow;
    [splashAd showSplashViewInRootViewController:keyWindow.rootViewController];
    NSLog(@"[Splash] Show splash view in rootViewController");
}

- (void)splashAdLoadFail:(BUSplashAd *)splashAd error:(BUAdError *_Nullable)error {
    NSLog(@"❌ [Splash] Load fail: %@", error.localizedDescription);
    self.splashAd = nil;
}

- (void)splashAdRenderSuccess:(BUSplashAd *)splashAd {
    NSLog(@"✅ [Splash] Render success");
    BUMRitInfo *ritInfo = [splashAd.mediation getShowEcpmInfo];
    [WDGroMoreTrackManager track:BMS_EVENT_AD_IMPRESSION
                          params:@{BMS_PARAM_WDS:BMS_WDS_AD_SHOW,
                                   BMS_PARAM_TAB1:ritInfo.adnName ?: @"unknown",
                                   BMS_PARAM_TAB2:ritInfo.slotID ?: @"",
                                   BMS_PARAM_TAB3:ritInfo.ecpm ?: @"0"}
                           dType:BMS_DTYPE_AD_ACTION
                            desc:BMS_WDS_AD_SHOW
                         options:[WDGroMoreTrackManager trackOptionsOnlySendBMS]];
}

- (void)splashAdDidClick:(BUSplashAd *)splashAd {
    NSLog(@"👉 [Splash] Ad clicked");
    BUMRitInfo *ritInfo = [splashAd.mediation getShowEcpmInfo];
    [WDGroMoreTrackManager track:BMS_EVENT_AD_CLICK
                          params:@{BMS_PARAM_WDS:BMS_WDS_AD_CLICK,
                                   BMS_PARAM_TAB1:ritInfo.adnName ?: @"unknown",
                                   BMS_PARAM_TAB2:ritInfo.slotID ?: @"",
                                   BMS_PARAM_TAB3:ritInfo.ecpm ?: @"0"}
                           dType:BMS_DTYPE_AD_ACTION
                            desc:BMS_WDS_AD_CLICK
                         options:[WDGroMoreTrackManager trackOptionsOnlySendBMS]];
}

- (void)splashAdViewControllerDidClose:(BUSplashAd *)splashAd {
    NSLog(@"[Splash] ViewController closed");
    self.splashAd = nil;
}

- (void)splashDidCloseOtherController:(BUSplashAd *)splashAd interactionType:(BUInteractionType)interactionType {
    NSLog(@"[Splash] Other controller closed, interactionType: %ld", (long)interactionType);
    [splashAd.mediation destoryAd];
    
    self.splashAd = nil;
}

- (void)splashAdDidClose:(BUSplashAd *)splashAd closeType:(BUSplashAdCloseType)closeType {
    NSLog(@"[Splash] splashAdDidClose with type: %ld", (long)closeType);
}

- (void)splashAdDidShow:(BUSplashAd *)splashAd {
    NSLog(@"[Splash] splashAdDidShow");
    
    BUMRitInfo *ritInfo = [splashAd.mediation getShowEcpmInfo];

    [WDGroMoreTrackManager track:BMS_EVENT_AD_IMPRESSION_DONE
                          params:@{BMS_PARAM_WDS:BMS_WDS_AD_FINISH,
                                   BMS_PARAM_TAB1:ritInfo.adnName ?: @"unknown",
                                   BMS_PARAM_TAB2:ritInfo.slotID ?: @"",
                                   BMS_PARAM_TAB3:ritInfo.ecpm ?: @"0"}
                           dType:BMS_DTYPE_AD_ACTION
                            desc:BMS_WDS_AD_FINISH
                         options:[WDGroMoreTrackManager trackOptionsOnlySendBMS]];
}

- (void)splashAdRenderFail:(BUSplashAd *)splashAd error:(BUAdError * _Nullable)error {
    NSLog(@"❌ [Splash] Render fail: %@", error.localizedDescription);
}

- (void)splashAdWillShow:(BUSplashAd *)splashAd {
    NSLog(@"[Splash] splashAdWillShow");
}

- (void)splashVideoAdDidPlayFinish:(BUSplashAd *)splashAd didFailWithError:(NSError * _Nullable)error {
    NSLog(@"[Splash] splashVideoAdDidPlayFinish, error: %@", error.localizedDescription);
}

- (void)splashCardReadyToShow:(BUSplashAd *)splashAd {
    NSLog(@"[Splash] splashCardReadyToShow");
}

- (void)splashCardViewDidClick:(BUSplashAd *)splashAd {
    NSLog(@"[Splash] splashCardViewDidClick");
}

- (void)splashCardViewDidClose:(BUSplashAd *)splashAd {
    NSLog(@"[Splash] splashCardViewDidClose");
}

@end
